/*
 * more online teacher js 
 */
var studyingPhasesList = [];
var subjectsList = [];
var primarySubjectList = [];
var showWaysList = [];

var studyingPhasesStr = "primarySchool,juniorSchool,seniorSchool";
var subjectsStr = "chinese,math,english,physics,chemistry,biology,politics,history,geography";
var primarySubjectStr = "primaryChinese,primaryMath,primaryEnglish";
var showWaysStr = "friendTeachers,highScore,moreAnswers";

$(document).ready(function(){
	studyingPhasesList = studyingPhasesStr.split(",");
	subjectsList = subjectsStr.split(",");
	primarySubjectList = primarySubjectStr.split(",");
	showWaysList = showWaysStr.split(",");
	
	var totalPageCount = document.getElementById("totalPageCount").value;
	var currentPageNum = document.getElementById("currentPageNum").value;
	
	var prePageCount = document.getElementById("prePageCount").value;
	if(null != prePageCount && parseInt(prePageCount) == 5){
		document.getElementById("prePageCount_5").selected="selected";
	}else{
		document.getElementById("prePageCount_10").selected="selected";
	}
	
	var options = {
		currentPage : parseInt(currentPageNum) + 1,
		totalPages : parseInt(totalPageCount),
		onPageClicked: function(e,originalEvent,type,page){
            jumpToChoosePage(page-1);
        },
		itemContainerClass: function (type, page, current) {
            return (page === current) ? "active" : "pointer-cursor";
        }
	}
	$('#pager').bootstrapPaginator(options);
	
	confirmStudyingPhase();
	
	confirmSubject();
	
	confirmShowWay();
 });


function jumpToChoosePage(currentPage) {
	var studyingPhase = document.getElementById("studyingPhase").value;
	var showWay = document.getElementById("showWay").value;
	var subject = document.getElementById("subject").value;
	
	studyingPhase = (null != studyingPhase) ? studyingPhase : 0;
	showWay = (null != showWay) ? showWay : 0;
	subject = (null != subject) ? subject : 0;

	location.href = getBasePath() + "/moreOnlineTeachers?currentPage=" + currentPage + "&studyingPhase=" + studyingPhase + "&showWay=" + showWay + "&subject=" + subject;
}

function goToPage(){
	var goPageNum = document.getElementById("goPageNum").value;
	var totalPageCount = document.getElementById("totalPageCount").value;
	
	if(parseInt(goPageNum) > parseInt(totalPageCount)){
		location.href = getBasePath() + "/moreOnlineTeachers?currentPage=0";
	}else{
		location.href = getBasePath() + "/moreOnlineTeachers?currentPage=" + (parseInt(goPageNum) - 1);
	}
}


function confirmStudyingPhase(){
	
	initInfoBtn(studyingPhasesList);
	
	var studyingPhase = document.getElementById("studyingPhase").value;
	if(null != studyingPhase){
		switch(parseInt(studyingPhase))
		{
		case 0:
			$("#primarySchool").addClass("btn-primary");
			$("#primarySchool").addClass("active");
			
			break;
		case 1:
			$("#juniorSchool").addClass("btn-primary");
			$("#juniorSchool").addClass("active");
			break;
		case 2:
			$("#seniorSchool").addClass("btn-primary");
			$("#seniorSchool").addClass("active");
			break;
		}
		
		if(parseInt(studyingPhase) == 0){
			$("#primarySubjects").css("display","block");
			$("#seniorAndJuniorSubjects").css("display","none");
		}else{
			$("#seniorAndJuniorSubjects").css("display","block");
			$("#primarySubjects").css("display","none");
		}
	}
}

function confirmSubject(){
	
	initInfoBtn(subjectsList);
	initInfoBtn(primarySubjectList);
	
	var studyingPhase = document.getElementById("studyingPhase").value;
	var subject = document.getElementById("subject").value;

	if(null != subject && null != studyingPhase && parseInt(studyingPhase) != 0){
		switch(parseInt(subject))
		{
		case 0:
			$("#chinese").addClass("btn-primary");
			$("#chinese").addClass("active");
			break;
		case 1:
			$("#math").addClass("btn-primary");
			$("#math").addClass("active");
			break;
		case 2:
			$("#english").addClass("btn-primary");
			$("#english").addClass("active");
			break;
		case 3:
			$("#physics").addClass("btn-primary");
			$("#physics").addClass("active");
			break;
		case 4:
			$("#chemistry").addClass("btn-primary");
			$("#chemistry").addClass("active");
			break;
		case 5:
			$("#biology").addClass("btn-primary");
			$("#biology").addClass("active");
			break;
		case 6:
			$("#politics").addClass("btn-primary");
			$("#politics").addClass("active");
			break;
		case 7:
			$("#history").addClass("btn-primary");
			$("#history").addClass("active");
			break;
		case 8:
			$("#geography").addClass("btn-primary");
			$("#geography").addClass("active");
			break;
		}
		
	}else{
		switch(parseInt(subject))
		{
		case 0:
			$("#primaryChinese").addClass("btn-primary");
			$("#primaryChinese").addClass("active");
			break;
		case 1:
			$("#primaryMath").addClass("btn-primary");
			$("#primaryMath").addClass("active");
			break;
		case 2:
			$("#primaryEnglish").addClass("btn-primary");
			$("#primaryEnglish").addClass("active");
			break;
		}
	}
}

function confirmShowWay(){
	
	initInfoBtn(showWaysList);
	
	var showWay = document.getElementById("showWay").value;
	if(null != showWay){
		switch(parseInt(showWay))
		{
		case 0:
			$("#friendTeachers").addClass("btn-primary");
			$("#friendTeachers").addClass("active");
			break;
		case 1:
			$("#highScore").addClass("btn-primary");
			$("#highScore").addClass("active");
			break;
		case 2:
			$("#moreAnswers").addClass("btn-primary");
			$("#moreAnswers").addClass("active");
			break;
		}
	}
}

function initInfoBtn(list){
	for(var i=0; i<list.length; i++){
		$("#" + list[i]).removeClass("btn-primary");
		$("#" + list[i]).removeClass("active");
	}
}

function chooseStudyingPhase(studyingPhase){
	var subject = document.getElementById("subject").value;
	var showWay = document.getElementById("showWay").value;
	
	studyingPhase = (null != studyingPhase) ? parseInt(studyingPhase) : 0;
	subject = (null != subject) ? parseInt(subject) : 0;
	
	if(studyingPhase == 0){
		subject = (subject > 2) ? 0 : subject;
	}
	
	jumpToChooseInfoPage(studyingPhase, subject, showWay);
}

function chooseSubject(subject){
	var studyingPhase = document.getElementById("studyingPhase").value;
	var showWay = document.getElementById("showWay").value;
	
	jumpToChooseInfoPage(studyingPhase, subject, showWay);
}

function chooseShowWay(showWay){
	var studyingPhase = document.getElementById("studyingPhase").value;
	var subject = document.getElementById("subject").value;
	
	jumpToChooseInfoPage(studyingPhase, subject, showWay);
}

function jumpToChooseInfoPage(studyingPhase, subject, showWay){
	subject = (null != subject) ? subject : 0;
	showWay = (null != showWay) ? showWay : 0;
	studyingPhase = (null != studyingPhase) ? studyingPhase : 0;
	
	location.href = getBasePath() + "/moreOnlineTeachers?studyingPhase=" + studyingPhase + "&subject=" + subject + "&showWay=" + showWay;
}